Micro SD Card Module and Arduino Interface

The majority of Microcontroller Units (MCU) include a limited amount of storage space that is adequate to hold your code or software. The Arduino Mega (ATmega2560) has 8 KB of bootloader space, 256 KB of flash memory for code storage, 8 KB of SRAM, and 4 KB of EEPROM (which can be read and written with the EEPROM library). The issue now is what to do if you need to store data for applications like data loggers, data visualization, etc., but the microcontroller unit memory is too small to be taken into consideration and only should be used to store your code. We need to use external memory to solve this issue, and SD cards are among the popular choices. Thus this article will quench your thirst for a Micro SD card module and Arduino interface.

SD (Secure Digital) is a non-volatile memory card popular due to its portability. Even among the different types of SD cards, microSD cards are used extensively in microcontroller applications due to their miniature size having a form factor of 15 mm × 11 mm × 1 mm. Too Facilitate communication between a microSD card and an Arduino board microSD card module is required, which should be easily accessible in the market.

Hardware Overview of MicroSD card module

Micro SD card module

Figure: Micro SD Card Module Front and Back Side

A MicroSD card mainly consists of three main components:

  • 3.3V LDO Voltage Regulator: The operating voltage of a standard microSD card is 3.3V, and a voltage greater than 3.6V will permanently damage the SD card. Thus, the module contains a 3.3V low dropout voltage regulator to provide a proper power supply to the SD card and to prevent damage from the 5V logic circuit.
  • Logic level shifter chip (74LVC125A): It resolves any issues that arise due to voltage incompatibility between the SD card module and a microcontroller.
  • MicroSD card Connector: It holds the microSD card and allows easy card insertion and removal.

MicroSD Card Module Pin Layout

S.No. Pin Type Pin Description
1 VCC Voltage Input
2 GND Ground
3 MISO Master In Slave Out (SPI Output)
4 MOSI Master Out Slave In (SPI Input)
5 SCK Serial Clock
6 CS Chip select

Micro SD Card Module and Arduino Interface

The first step is to correctly format the microSD card to FAT16 or FAT32 before placing it in the module. Afterward, connect the module VCC and GND pin to the 3.3V or 5V pin and GND pin of the Arduino respectively.

Micro SD Card Module and Arduino Interface

Interfacing of the SD card module with Arduino is done with Serial Peripheral Interface (SPI). The SPI pins of the module are connected to the Arduino’s SPI pins. Different models of Arduino boards have different SPI pins, so ensure that SPI pins are connected correctly.

SPI pins of Arduino mega, Arduino uno, and Arduino nano 

S.No. Pin Type Arduino mega Arduino uno Arduino nano
1 MISO 50 12 12
2 MOSI 51 11 11
3 SCK 52 13 13
4 CS 53 4 4

Preparation of SD Card

To prepare the SD card for interfacing, we have to format it in the right system. Initially, though the SD card is formatted in the FAT file system but it might not work. For proper working of SD card, you have to format it into FAT16 or FAT32 system.

SD card format in fat system

Figure: Micro SD card Format in FAT system

Arduino Code

First program: Testing the SD card

Arduino IDE provides an in-built Cardinfo Sketch to test your SD card. When Sketch is uploaded to the Arduino depending upon the output displayed on the serial monitor you may infer the following things.

  • SD card is working properly or not
  • SD card is corrupted or not
  • SD card is properly formatted or not

Before Uploading the sketch, verify that the CS(chip select) pin is initialized correctly. 

To open the CardInfo example sketch, navigate to File > Examples > SD > CardInfo.

arduino SD card example Figure: Arduino example for SD card 

SD card infor

Figure: SD card information

After successfully verifying the SD card, we are ready to perform the read-and-write operation.

After the Sketch is uploaded serial monitor will display

arduino sd card read and write

Figure: SD card Read and Write output

Code Explanation

This code block starts with importing the SD and SPI library which enables the communication between the SD module and Arduino through the SPI interface.

Next, comes the definition CS (chip select) pin, and other SPI pins are defined in the library as we are using hardware interrupt. Finally, we declare our file object to access the methods for read and write operations.

This block of code simply checks if the SD is recognized or not after the Serial monitor is up and running.

This block of code performs the write operation on the file and if the file cannot be opened an error message is displayed on the serial monitor. Here, open() function is used to open the file. File can be opened in different modes two modes mentioned in this code are

  • FILE_WRITE (opens the file for reading and writing and places the cursor at the end of the file). 
  • FILE_READ (opens the file for reading and places the cursor at the beginning of the file)

To write on a file it must be open in write mode and println() function can be used to write on the file. After the write operation, we must close the file which is accomplished by calling close() methods on file objects. 

This block of code opens the file in read mode and prints every character on the serial monitor. Here, the read() function reads a single character at a time so a while loop is used to read all the characters of the file.

Leave a Comment